package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.GameUtil;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.List;

public class Item extends AbstractMapleStoryObject {

    public int type;
    public Image image;
    public boolean jump;

    public Item() {
        this.jump = true;
        this.live = true;
    }

    public int quantity = 1;
    public Item(MapleStoryClient msc, int x, int y, int type) {
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.type = type;
        switch (type) {
            case 0:
                this.image = ImageUtil.getImage("HP_100");
                break;
            case 1:
                this.image = ImageUtil.getImage("HP_300");
                break;
            case 2:
                this.image = ImageUtil.getImage("MP_100");
                break;
            case 3:
                this.image = ImageUtil.getImage("MP_300");
                break;
            case 4:
                this.image = ImageUtil.getImage("AT_300");
                break;
            case 5:
                this.image = ImageUtil.getImage("AT_500");
                break;
            default:
                break;

        }
        this.width = 30;
        this.height = 30;
    }
    @Override
    public void draw(Graphics g) {
        if (!live) {
            msc.itemList.remove(this);
            return;
        }
        g.drawImage(image, x, y, null);
        move();
    }

    @Override
    public void move() {
        if (jump) {
            jump(msc.grounds);
        }
    }

    /**
     * 道具出来跳一下
     */
    private double v0 = Constant.INIT_ITEM_JUMP_SPEED;
    private double vt;
    private double g = 9.8;
    private double delta_height;
    private double t = 0.5;

//    private void jump() {
//        vt = v0 - g * t;
//        v0 = vt;
//        delta_height = v0 * t;
//        y -= delta_height;
//        if (y >= 550) {
//            jump = false;
//            v0 = Constant.INIT_ITEM_JUMP_SPEED;
//            vt = 0.0;
//            y = 550;
//        }
//    }
    private boolean drop;
    public void jump(List<Ground> grounds){
        if (!drop){
            vt = v0 - g * t;
            delta_height = v0 * t;
            v0 = vt;
            y -= delta_height;
            if (vt <=0){
                vt = 0.0;
                drop = true;
            }
        }else {
            jumpDown(grounds);
        }
    }
public void jumpDown(List<Ground> grounds){
    vt = v0 + g * t;
    delta_height = v0 * t;
    v0 = vt;
    y += delta_height;
    for (int i = 0; i < grounds.size(); i++) {
        Ground ground = grounds.get(i);
        if (this.getRectangle().intersects(ground.getRectangle()) && this.y<=ground.y - this.height  + 80){
            if (this.y >= (ground.y - this.height)){
                this.y = ground.y - this.height;
                drop = false;
                jump = false;
                v0 = Constant.INIT_JUMP_SPEED;
                vt = 0.0;
                break;
            }
        }else {
            drop = true;
        }
    }
}
}
